#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2021-01-18 10:11 上午
# @Author  : yangdy
# @File    : 1021.删除最外层的括号
# @Software: PyCharmCE
from tools.my_methods import *


class Solution:
    def removeOuterParentheses(self, S: str) -> str:
        # data = S.split(')(')
        # print(data)
        # return ''.join([data[0][1:]] + data[1:len(data)-1] + [data[-1][:-1]])
        ans, c = [], 0
        for s in S:
            if s == '(' and c > 0:
                ans.append(s)
            if s == ')' and c > 1:
                ans.append(s)
            c += 1 if s == '(' else -1
        return ''.join(ans)


questions = [
    ("(()())(())",),
    ("(()())(())(()(()))",),
    ("()()",),
]

work(questions, lambda q: Solution().removeOuterParentheses(q[0]))
